//
//  GBSingleWebViewController.m
//  警用舆情平台
//
//  Created by 郭斌 on 15/5/13.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "GBSingleWebViewController.h"
#import "KDReportController.h"
#import "KDPatrolgetReportParam.h"
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>
#import "GBRequest.h"
#import "SVProgressHUD.h"
#import "WXApi.h"
#define docid @"&docId="
#define favid @"&favId="
#define smiddd @""
#define keyword @"&keyword="
#define collectUrl @"method=addFavorite&token="
#define cancleCollectUrl @"method=deleteFav&token="
//#define ServerActionAddr3 @"http://192.168.1.221/MView.html?"
//#define ServerActionAddr2 @"http://ql.yunyq.cn/MView.html?"
#import "SVProgressHUD.h"

@interface GBSingleWebViewController () <UIWebViewDelegate,MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate,GBRequestDelegate>
{
    UIWebView *webview; //webview
    //5.18改
    UIView *shareView; //分享菜单
    UIButton *reportBtn; //刷新按钮
    UIButton *collectBtn; //收藏按钮
    UIButton *weixinBtn; //微信按钮
    UIButton *duanxinBtn; //短信按钮
    UILabel *collectLabel; //短信字体
    UILabel *refreshLabel ;
}

//webView需要的url
@property (nonatomic, copy) NSString * webViewUrl;
/** *  当前网页的url */
@property (nonatomic, copy) NSString * currentUrl;

@property (nonatomic, strong) UIButton *rightBarBtn;

/**
 *  自定义navBar
 */
@property (nonatomic, strong) UIView *navbar;
@property (nonatomic, weak) UIButton *backView;
/**
 *  右滑手势
 */
@property (nonatomic, strong) UIPanGestureRecognizer *rightSwipeGestureRecognizer;
@property (nonatomic, assign) BOOL isBackMain;

@end

@implementation GBSingleWebViewController

#pragma mark - 版面设置区
- (void)viewDidLoad {
    [super viewDidLoad];
    //监听上报成功之后的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReportControllerReportSeccess:) name:@"ReportControllerReportSeccessToSingleNoty" object:nil];
    
    self.view.backgroundColor = BackGroundColor;
    [self setUpRightBarBtn];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    //KDLog(@"%@",_whichPage);
    self.navigationItem.title = self.titleee;
    //初始化web
    [self createWebview];
    //初始化分享界面
    [self createShareView];
    //添加左滑手势
    self.rightSwipeGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer.minimumNumberOfTouches = 2;
    [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
//    [self setUpBackMainPageBtn];
}
//设置状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    
    return UIStatusBarStyleLightContent;
}

/**
 *  设置中间的titleView
 */
- (void)setUpTitleView{
    UILabel *titleView = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, screenW - CGRectGetMaxX(self.backView.frame) *2, self.navbar.height)];
    titleView.text = self.titleee;
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.textColor = [UIColor whiteColor];
    titleView.font = [UIFont boldSystemFontOfSize:18];
    titleView.centerX = self.navbar.centerX;
    titleView.centerY = self.navbar.centerY +10;
    [self.navbar addSubview:titleView];
}
/***  创建右边刷新按钮 */
- (void)setUpRightBarBtn
{
    UIButton *rightBarBtn = [[UIButton alloc]init];
    self.rightBarBtn = rightBarBtn;
    UIImage *image = [UIImage imageNamed:@"刷新"];
    rightBarBtn.width = image.size.width ;
    rightBarBtn.height = image.size.height;
    rightBarBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBarBtn setImage:image forState:UIControlStateNormal];;
    [rightBarBtn addTarget:self action:@selector(rightBarBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    if ([self.comeFrom isEqualToString:@"3"]) {
//        rightBarBtn.x = screenW -  self.rightBarBtn.width - 10;
//        rightBarBtn.y = 20 + (44 - rightBarBtn.height)* 0.5;
//        [self.navbar addSubview:rightBarBtn];
//    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBarBtn];
//    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewClick)];
}


#pragma mark - 创建webview
-(void)createWebview
{
    KDAccount *account = [KDAccountTool account];
    webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH - 49 - 64)];
    //KDLog(@"%@",_url);
    webview.backgroundColor = BackGroundColor;
    self.webViewUrl = [NSString stringWithFormat:@"%@MView.html?token=%@&id=%@&tid=%ld&type=%d&LoginType=mobile", ServerActionAddr2,account.tokens, _docid,  (long)self.tid, self.type];
//    self.webViewUrl = @"https://www.youtube.com/";
    KDLog(@"%@",_webViewUrl);
    webview.scrollView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshView)];
    webview.scalesPageToFit = YES;
    webview.delegate = self;
    [self.view addSubview: webview];
    webview.scrollView.bounces = YES;
    //设置滚动条的显示
    //水平滚动条
    webview.scrollView.showsHorizontalScrollIndicator = NO;
    //垂直滚动条
    webview.scrollView.showsVerticalScrollIndicator = NO;
    [self loadData];
    [self.view addSubview:webview];
}

#pragma mark - 初始化分享菜单
-(void)createShareView
{
    shareView = [[UIView alloc] initWithFrame:CGRectMake(0, screenH - 49 - 64, screenW, 49)];
    shareView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    shareView.layer.borderWidth = 0.3;
    shareView.backgroundColor = WQColor(248, 248, 248);
    
    reportBtn = [UIButton buttonWithType:0];
    UIImage *image = [UIImage imageNamed:@"上报icon"];
    [reportBtn addTarget:self action:@selector(reportBtnClick) forControlEvents:UIControlEventTouchUpInside];
    reportBtn.imageEdgeInsets = UIEdgeInsetsMake(-8, 0, 8, 0);
    reportBtn.frame = CGRectMake(31, 0, image.size.width, 49);
    [reportBtn setImage: image forState:UIControlStateNormal];
    [reportBtn setImage:[UIImage imageNamed:@"上报选中icon"] forState:UIControlStateSelected];
    
//    refreshBtn.backgroundColor = RandomColor;
    refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(reportBtn.x, 22 -5.5, reportBtn.width, 40)];
    
    refreshLabel.font = [UIFont systemFontOfSize:11];
    refreshLabel.textAlignment = NSTextAlignmentCenter;
    refreshLabel.textColor = [UIColor grayColor];
    reportBtn.selected = _report;
    KDAccount *account = [KDAccountTool account];
    
    [self resetReportLabel];
    int i = 0;
    CGFloat btnNumber = (account.version) ? 3.0:4.0;
    if (!account.version) {
        [shareView addSubview:reportBtn];
        [shareView addSubview:refreshLabel];
        i++;
    }
    
    
//    refreshLabel.backgroundColor = RandomColor;
    collectBtn = [UIButton buttonWithType:0];
    [collectBtn setImage:[UIImage imageNamed:@"web收藏"] forState:UIControlStateNormal];
    [collectBtn setImage:[UIImage imageNamed:@"web收藏选中"] forState:UIControlStateSelected];
    [collectBtn addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
    collectBtn.frame = CGRectMake((screenW / btnNumber) * i + 26 , 0, image.size.width, 49);
    collectBtn.x = (account.version)?(screenW / btnNumber) * i + 50 :(screenW / btnNumber) * i + 26;
    collectBtn.imageEdgeInsets = UIEdgeInsetsMake(-8, 0, 8, 0);

    collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(collectBtn.x, 22 -5.5, collectBtn.width, 40)];
    collectLabel.text = @"收藏";
    collectLabel.font = [UIFont systemFontOfSize:11];
    collectLabel.textAlignment = NSTextAlignmentCenter;
    //判断是否已经收藏过
    if(_isCollect == 0)
    {
        collectBtn.selected = NO;
        collectLabel.textColor = [UIColor grayColor];
    }
    else
    {
        collectBtn.selected = YES;
        collectLabel.textColor = WQColor(254, 123, 36);}
    
    i++;
    weixinBtn = [UIButton buttonWithType:0];
    [weixinBtn setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
    [weixinBtn addTarget:self action:@selector(weixinShare) forControlEvents:UIControlEventTouchUpInside];
    weixinBtn.frame = CGRectMake((screenW / btnNumber) * i + 25, collectBtn.y, collectBtn.width, collectBtn.height);
    weixinBtn.imageEdgeInsets = UIEdgeInsetsMake(-8, 0, 8, 0);

    UILabel *weixinLabel = [[UILabel alloc] initWithFrame:CGRectMake(weixinBtn.x, 22 - 5.5, collectBtn.width, 40)];
    weixinLabel.text = @"微信";
    weixinLabel.font = [UIFont systemFontOfSize:11];
    weixinLabel.textAlignment = NSTextAlignmentCenter;
    weixinLabel.textColor = [UIColor grayColor];
//    weixinLabel.backgroundColor = RandomColor;
    
    i++;
    duanxinBtn = [UIButton buttonWithType:0];
    [duanxinBtn setImage:[UIImage imageNamed:@"信息"] forState:UIControlStateNormal];
    [duanxinBtn addTarget:self action:@selector(duanxinShare) forControlEvents:UIControlEventTouchUpInside];
    duanxinBtn.frame = CGRectMake((screenW / btnNumber) * i + 25, collectBtn.y, collectBtn.width, collectBtn.height);
    duanxinBtn.imageEdgeInsets = UIEdgeInsetsMake(-8, 0, 8, 0);

    UILabel *duanxinLabel = [[UILabel alloc] initWithFrame:CGRectMake(duanxinBtn.x, 22 - 5.5, collectBtn.width, 40)];
    duanxinLabel.text = @"信息";
    duanxinLabel.font = [UIFont systemFontOfSize:11];
    duanxinLabel.textAlignment = NSTextAlignmentCenter;
    duanxinLabel.textColor = [UIColor grayColor];
    
    [shareView addSubview:weixinBtn];
    [shareView addSubview:duanxinBtn];
    
    [shareView addSubview:collectBtn];
    
    [shareView addSubview:collectLabel];
    [shareView addSubview:weixinLabel];
    [shareView addSubview:duanxinLabel];
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [self.view addSubview:shareView];
}
/**
 *  刷新上报按钮下面字的状态
 */
- (void)resetReportLabel{
    refreshLabel.text = (_report) ? @"已上报":@"上报";
    refreshLabel.width = (_report)?reportBtn.width +10:reportBtn.width;
    refreshLabel.centerX = reportBtn.centerX;
    refreshLabel.textColor = (_report)? KDHeadColor :[UIColor grayColor];
}
#pragma mark - 事件点击区

/**
 *  后退按钮被点击了
 */
- (void)backViewClick{
    if (self.isBackMain) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self loadData];
    }
    
}
/**
 *  上报按钮被点击了
 */
- (void)reportBtnClick{
    KDReportController *reportCtrl = [[KDReportController alloc]init];
    KDPatrolgetReportParam *param = [[KDPatrolgetReportParam alloc]init];
    param.ori_url = self.url;
    param.ori_title = self.titleee;
    param.ori_content = self.ori_content;
    param.st = self.st;
    param.ori_author = self.ori_author;
    param.ori_webname = self.ori_webname;
    param.doc_id = _docid;
    reportCtrl.param = param;
    reportCtrl.type = 4;
    reportCtrl.singelFromType = [self.comeFrom integerValue];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:reportCtrl animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
}
/**
 *  刷新按钮的操作
 */
-(void)rightBarBtnClick
{
    [webview.scrollView.header beginRefreshing];
    [self loadData];
    //KDLog(@"1");
    
}
- (void)refreshView{
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:_currentUrl]];
    [webview loadRequest:request];
}
- (void)loadData{
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:_webViewUrl]];
    [webview loadRequest:request];
}
- (void)setReport:(BOOL)report{
    _report = report;
    reportBtn.selected = _report;
}
/**
 *  收藏按钮的操作
 */
-(void)collectAction
{
    KDAccount *account = [KDAccountTool account];
    NSString *uuuuuuall = nil;
    if(collectBtn.selected)
    {
        //取消收藏
        uuuuuuall = [NSString stringWithFormat:@"%@&%@%@%@%@", ServerActionAddr, cancleCollectUrl,account.tokens,docid,_docid];}
    else
    {
        //点击收藏
        uuuuuuall = [NSString stringWithFormat:@"%@&%@%@%@%@%@%@",ServerActionAddr, collectUrl,account.tokens,docid,_docid,@"&url=",_url];
    }
    KDLog(@"%@",uuuuuuall);
    GBRequest *request1 = [[GBRequest alloc]initWithUrlStr:uuuuuuall];
    request1.tag = 3;
    request1.delegate = self;
    [request1 startRequestData];
}

- (void)handleSwipes:(UIPanGestureRecognizer *)gesture{
    
    if (gesture.state == UIGestureRecognizerStateChanged )
    {
        [self backViewClick];
    }
}
#pragma mark - 网络请求区
/**
 *  数据成功请求完毕
 */
- (void)requestDidFinished:(GBRequest *)request
{
    NSString *str = [[NSString alloc] initWithData:request.requestData encoding:NSUTF8StringEncoding];
    //解析
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    id dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *jsonDict = nil;
    //KDLog(@"%@",jsonDict);
    if ([dataArray isKindOfClass:[NSDictionary class]])
    {
        jsonDict = dataArray;
        if ([jsonDict[@"status"] isEqualToString:@"ok"]) {//请求成功
            collectBtn.selected = !collectBtn.selected;
            if (!collectBtn.selected) {
                collectLabel.textColor = [UIColor grayColor];
                [SVProgressHUD showSuccessWithStatus:@"取消成功" maskType:SVProgressHUDMaskTypeGradient];
            }else{
                collectLabel.textColor = WQColor(254, 123, 36);
                [SVProgressHUD showSuccessWithStatus:@"收藏成功" maskType:SVProgressHUDMaskTypeGradient];
            }
            //请求成功就要发送收藏按钮被点击的通知
            if ([self.comeFrom isEqualToString:@"1"]) {//预警页面点进来的收藏
                //                KDLog(@"%d", collectBtn.selected);
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SingleWebViewControllerCollectionBtnClickToHomeNoty" object:nil userInfo:@{@"isCollect":@(collectBtn.selected), @"docid":_docid}];
            }else if ([self.comeFrom isEqualToString:@"2"]){//巡查界面页面点进来的收藏
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SingleWebViewControllerCollectionBtnClickToPatrolNoty" object:nil userInfo:@{@"isCollect":@(collectBtn.selected), @"docid":_docid}];
            }else if ([self.comeFrom isEqualToString:@"3"]){//单个通知界面页面点进来的收藏
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SingleWebViewControllerCollectionBtnClickToSingleNotyNoty" object:nil userInfo:@{@"isCollect":@(collectBtn.selected), @"docid":_docid}];
            }else if ([self.comeFrom isEqualToString:@"4"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SingleWebViewControllerCollectionBtnClickToMonitorNoty" object:nil userInfo:@{@"isCollect":@(collectBtn.selected), @"docid":_docid}];
            }else if ([self.comeFrom isEqualToString:@"5"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SingleWebViewControllerCollectionBtnClickToSearchNoty" object:nil userInfo:@{@"isCollect":@(collectBtn.selected), @"docid":_docid}];
            }
        }else{//请求失败
            if (collectBtn.selected) {
                [SVProgressHUD showSuccessWithStatus:@"取消失败" maskType:SVProgressHUDMaskTypeGradient];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"收藏失败" maskType:SVProgressHUDMaskTypeGradient];
            }
        }
        //KDLog(@"%@",dict[@"status"]);
    }
}
/**
 *  数据成功请求失败
 */
- (void)requestDidFailedWithError:(NSError *)error
{
    //关闭提示消息的进度圈
    if (collectBtn.selected) {
        [SVProgressHUD showSuccessWithStatus:@"取消失败" maskType:SVProgressHUDMaskTypeGradient];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"收藏失败" maskType:SVProgressHUDMaskTypeGradient];
    }
}
#pragma mark - 微信分享界面生成
/**
 *  分享成功之后干啥
 */
-(void)onResp:(BaseResp *)resp
{
    KDLog(@"%@", resp);
}
#pragma mark - 微信分享
-(void)weixinShare
{
    BOOL IsInstall = [WXApi isWXAppInstalled];
    if(!IsInstall)
    {
        [SVProgressHUD showErrorWithStatus:@"用户未安装微信应用" maskType:SVProgressHUDMaskTypeGradient];
        return;
    }
    
    WXMediaMessage *message = [WXMediaMessage message];
    
    WXWebpageObject *web = [WXWebpageObject object];
    web.webpageUrl = _url;
    message.mediaObject = web;
    
    message.title = _titleee;
    
    SendMessageToWXReq *sendMsg = [[SendMessageToWXReq alloc] init];
    sendMsg.message = message;
    sendMsg.scene = WXSceneSession;
    [WXApi sendReq:sendMsg];
    
//  [self doTHeWXsendTheURL];
//  KDLog(@"微信分享");
}

//#pragma mark - 微信分享url
//-(void)doTHeWXsendTheURL
//{
//    //NSUserDefaults
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    //读取数据
//    NSString *str = [userDefaults stringForKey:@"TokenMain"];
//
////    NSString *uuuuuuall = [NSString stringWithFormat:@"%@%@%@%@%@%@",baseUrl2,__iddddd,smiddd,__midddd,@"&token=",str];
//    //KDLog(@"%@",uuuuuuall);
//    GBRequest *request1 = [[GBRequest alloc]initWithUrlStr:uuuuuuall];
//    request1.tag = 1;
//    request1.delegate = self;
//    [request1 startRequestData];
//    //KDLog(@"%@",uuuuuuall);
//}

#pragma mark - 短信分享
-(void)duanxinShare
{
    [self showSMSPicker];
    [self doTheSendUrl];
}

#pragma mark - 短信分享操作
-(void)showSMSPicker{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    
    if (messageClass != nil)
    {
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText])
        {
            [self displaySMSComposerSheet];
        }
        else
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"设备不支持短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            
        }
    }
    else
    {
        
    }
}

#pragma mark - 出现短信发送界面
-(void)displaySMSComposerSheet
{
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate =self;
    NSString *smsBody =[NSString stringWithFormat:@"%@%@",_titleee,_url];
    picker.body=smsBody;
    //[self presentModalViewController:picker animated:YES];
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - 短信发送可能出现的情况及结果
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result)
    {
        case MessageComposeResultCancelled:
            break;
        case MessageComposeResultSent:
            //[self doTheSendUrl];
            break;
        case MessageComposeResultFailed:
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 发送url
-(void)doTheSendUrl
{
//    //NSUserDefaults
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    //读取数据
//    NSString *str = [userDefaults stringForKey:@"TokenMain"];
//    
//    NSString *uuuuuuall = [NSString stringWithFormat:@"%@%@%@%@%@%@",baseUrl,__iddddd,smiddd,__midddd,@"&token=",str];
//    //KDLog(@"%@",uuuuuuall);
//    GBRequest *request1 = [[GBRequest alloc]initWithUrlStr:uuuuuuall];
//    request1.tag = 2;
//    request1.delegate = self;
//    [request1 startRequestData];
    //KDLog(@"%@",uuuuuuall);
}

#pragma mark - 页面即将消失时清空缓存
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //清空当前缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webview.scrollView.header endRefreshing];
    //修改服务器页面的meta的值
//    NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", webView.frame.size.width];
//    [webView stringByEvaluatingJavaScriptFromString:meta];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    KDLog(@"%@", error);
    [webview.scrollView.header endRefreshing];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *urlString = request.URL.absoluteString;
    self.currentUrl = urlString;
    if ([urlString rangeOfString:self.webViewUrl].location != NSNotFound) {//
        self.isBackMain = YES;
    }else{
        self.isBackMain = NO;
    }
    return YES;
}

#pragma mark - 通知接收区
- (void)ReportControllerReportSeccess:(NSNotification *)noty{
    NSString *docId = noty.userInfo[@"docid"];
    if ([docId isEqualToString: _docid]) {
        self.report = YES;
        reportBtn.selected = _report;
        [self resetReportLabel];
    }
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
